package gui.panel_new;

import enums.TextBundleEnum;
import enums.URLEnum;
import enums.VennEnum;
import misc.BundlesProvider;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Grafica un diagrama de Venn;
 */
public class BasicVennPanel extends JPanel {
    private Area c1, c2, c3, c4, c5, c6, c7;
    private Ellipse2D circle1;
    private Ellipse2D circle2;
    private Ellipse2D circle3;
    private Map<Integer, Boolean> crossMap;
    private Map<Integer, Boolean> fillMap;
    private String terminoMayor;
    private String terminoMedio;
    private String terminoMenor;
    private ResourceBundle textBundle;

    /**
     * Constructor;
     */
    public BasicVennPanel() {
        crossMap = new HashMap<Integer, Boolean>();
        fillMap = new HashMap<Integer, Boolean>();
        circle1 = new Ellipse2D.Double();
        circle2 = new Ellipse2D.Double();
        circle3 = new Ellipse2D.Double();
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        terminoMayor = textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString());
        terminoMedio = textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString());
        terminoMenor = textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString());
    }

    /**
     * Pinta el diagrama de Venn;
     *
     * @param g;
     */
    public void paint(Graphics g) {
        double width = getWidth() * 1.33;
        double height = getHeight() * 1.33;
        setBackground(Color.WHITE);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, (int) width, (int) height);
        circle1.setFrame(0, 0, width / 2, height / 2);
        c1 = new Area(circle1);
        if (fillMap.get(1) != null && fillMap.get(1)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.BLUE);
        }
        g2.fill(c1);

        circle2.setFrame(width / 4, 0, width / 2, height / 2);
        c3 = new Area(circle2);
        if (fillMap.get(3) != null && fillMap.get(3)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.YELLOW);
        }
        g2.fill(c3);
        circle3.setFrame(width / 8, height / 4, width / 2, height / 2);

        c7 = new Area(circle3);
        if (fillMap.get(7) != null && fillMap.get(7)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.MAGENTA);
        }
        g2.fill(c7);

        c2 = new Area(circle1);
        c2.intersect(c3);
        if (fillMap.get(2) != null && fillMap.get(2)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.GREEN);
        }
        g2.fill(c2);

        c4 = new Area(circle1);
        c4.intersect(c7);
        if (fillMap.get(4) != null && fillMap.get(4)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.LIGHT_GRAY);
        }
        g2.fill(c4);

        c6 = new Area(circle2);
        c6.intersect(c7);
        if (fillMap.get(6) != null && fillMap.get(6)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.GRAY);
        }
        g2.fill(c6);

        c5 = new Area(circle2);
        c5.intersect(c4);
        if (fillMap.get(5) != null && fillMap.get(5)) {
            g2.setColor(new Color(200, 128, 255));
        } else {
            g2.setColor(Color.WHITE);
        }
        g2.fill(c5);

        for (int j = 0; j < 7; j++) {
            if (crossMap.get(j + 1) != null && crossMap.get(j + 1).booleanValue()) {
                paintCross(j + 1, g2);
            }
        }
        paintVennStrings(g2, width, height);
    }

    /**
     * Pinta los Strings sobre el diagrama de Venn;
     *
     * @param graphics2D;
     * @param width;
     * @param height;
     */
    private void paintVennStrings(Graphics2D graphics2D, double width, double height) {
        graphics2D.setColor(Color.BLACK);

        graphics2D.drawString(terminoMayor, 5, 15);
        graphics2D.drawString(terminoMedio, (int) width / 3 + (int) width / 8, 15);
        graphics2D.drawString(terminoMenor, (int) width / 3 + (int) width / 8, (int) height / 2 + (int) height / 4);

        graphics2D.drawString("1", (int) width / 8, (int) width / 4);
        graphics2D.drawString("2", (int) width / 3 + 10, (int) width / 5);
        graphics2D.drawString("3", (int) width / 2 + (int) width / 8 - 11, (int) width / 4);
        graphics2D.drawString("4", (int) width / 4, (int) width / 4 + (int) width / 6);
        graphics2D.drawString("5", (int) width / 3 + 10, (int) width / 4 + (int) width / 6 - 5);
        graphics2D.drawString("6", (int) width / 2 - 2, (int) width / 4 + (int) width / 6);
        graphics2D.drawString("7", (int) width / 3 + 10, (int) width / 2 + (int) width / 5 - 10);
    }

    /**
     * Metodo qu pinta una cruz;
     *
     * @param section;
     * @param graphics2D;
     */
    public void paintCross(int section, Graphics2D graphics2D) {
        graphics2D.setColor(Color.BLACK);
        int x = (int) (getWidth() * 1.33);
        int y = (int) (getHeight() * 1.33);

        switch (section) {
            case 1:
                x = x / 10;
                y = y / 7;
                break;
            case 2:
                x = x / 3 + 2;
                y = y / 10;
                break;
            case 3:
                x = x / 2 + 20;
                y = y / 7;
                break;
            case 4:
                x = x / 8 + 30;
                y = y / 3 + 25;
                break;
            case 5:
                x = x / 3 + 2;
                y = y / 3 - 10;
                break;
            case 6:
                x = x / 2 - 10;
                y = y / 3 + 25;
                break;
            case 7:
                x = x / 3 + 2;
                y = y / 2 + 20;
                break;
        }
        graphics2D.drawLine(x + 0, y + 0, x + 20, y + 20);
        graphics2D.drawLine(x + 0, y + 1, x + 20, y + 21);
        graphics2D.drawLine(x + 0, y + 21, x + 20, y + 1);
        graphics2D.drawLine(x + 1, y + 19, x + 19, y + 1);
    }

    /**
     * Permite setear el termino mayor del silogismo representado;
     *
     * @param terminoMayor;
     */
    public void setTerminoMayor(String terminoMayor) {
        this.terminoMayor = terminoMayor;
    }

    /**
     * Permite setear el termino medio del silogismo representado;
     *
     * @param terminoMedio;
     */
    public void setTerminoMedio(String terminoMedio) {
        this.terminoMedio = terminoMedio;
    }

    /**
     * Permite setear el termino menor del silogismo representado;
     *
     * @param terminoMenor;
     */
    public void setTerminoMenor(String terminoMenor) {
        this.terminoMenor = terminoMenor;
    }

    /**
     * Permite setear los valores de VennEnum propios del diagrama;
     *
     * @param vennEnums;
     */
    public void setVennEnums(VennEnum[] vennEnums) {
        fillMap = new HashMap<Integer, Boolean>();
        crossMap = new HashMap<Integer, Boolean>();
        for (int j = 0; j < 7; j++) {
            switch (vennEnums[j]) {
                case CROSS:
                    crossMap.put(j + 1, true);
                    break;
                case FILL:
                    fillMap.put(j + 1, true);
                    break;
            }
        }
        repaint();
    }

    /**
     * Permite agregar una cruz al diagrama;
     *
     * @param section;
     */
    public void addCross(int section) {
        crossMap.put(section, true);
        fillMap.put(section, false);
    }

    /**
     * Permite rellenar una seccion;
     *
     * @param section
     */
    public void fillSection(int section) {
        fillMap.put(section, true);
        crossMap.put(section, false);
    }

    /**
     * Permite eliminar una cruz;
     *
     * @param section
     */
    public void deleteCross(int section) {
        crossMap.put(section, false);
    }

    /**
     * Permite borrar el pintado de una seccion;
     *
     * @param section
     */
    public void unpaintSection(int section) {
        fillMap.put(section, false);
    }

    public static void main(String[] args) {
        BasicVennPanel vennPanel = new BasicVennPanel();
        vennPanel.setPreferredSize(new Dimension(230, 230));
        JFrame frame = new JFrame();

        frame.setContentPane(vennPanel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        vennPanel.addCross(1);
        vennPanel.addCross(2);
        vennPanel.addCross(3);
        vennPanel.addCross(4);
        vennPanel.addCross(5);
        vennPanel.addCross(6);
        vennPanel.addCross(7);
    }
}
